<?php

  // get the form inputs
  $nombre = get_input('nombre');
  $descripcion = get_input('descripcion');
  $acceso = get_input('acceso');
  $escuela_guid = get_input('escuela_guid');

 
  // crear un nuevo objeto escuela
  $sector = new ElggObject();
  $sector->subtype = "sector";
  $sector->title = $nombre;
  $sector->description = $descripcion;

  	 
  // for now make all blog posts public
  $sector->access_id = ACCESS_PUBLIC;
 
  // owner is logged in user
  $sector->owner_guid = 1;
  //elgg_get_logged_in_user_guid();
 
  // save tags as metadata
 
  $sector_guid = $sector->save();
  

  $sector->acceso = $acceso;
  $sector->escuela_guid = $escuela_guid;
  
  $sector->save();
  
  /////////////////////////////////////////////////////////////////////////////////////////
  // Now see if we have a file icon
  if ((isset($_FILES['icon'])) && (substr_count($_FILES['icon']['type'],'image/'))) {
  
  	$icon_sizes = elgg_get_config('icon_sizes');
  
  	$prefix = "sectores/" . $sector_guid;
  
  	$filehandler = new ElggFile();
  	$filehandler->owner_guid = $sector->owner_guid;
  	$filehandler->setFilename($prefix . ".jpg");
  	$filehandler->open("write");
  	$filehandler->write(get_uploaded_file('icon'));
  	$filehandler->close();
  
  	$thumbtiny = get_resized_image_from_existing_file($filehandler->getFilenameOnFilestore(), $icon_sizes['tiny']['w'], $icon_sizes['tiny']['h'], $icon_sizes['tiny']['square']);
  	$thumbsmall = get_resized_image_from_existing_file($filehandler->getFilenameOnFilestore(), $icon_sizes['small']['w'], $icon_sizes['small']['h'], $icon_sizes['small']['square']);
  	$thumbmedium = get_resized_image_from_existing_file($filehandler->getFilenameOnFilestore(), $icon_sizes['medium']['w'], $icon_sizes['medium']['h'], $icon_sizes['medium']['square']);
  	$thumblarge = get_resized_image_from_existing_file($filehandler->getFilenameOnFilestore(), $icon_sizes['large']['w'], $icon_sizes['large']['h'], $icon_sizes['large']['square']);
  	if ($thumbtiny) {
  
  		$thumb = new ElggFile();
  		$thumb->owner_guid = $sector->owner_guid;
  		$thumb->setMimeType('image/jpeg');
  
  		$thumb->setFilename($prefix."tiny.jpg");
  		$thumb->open("write");
  		$thumb->write($thumbtiny);
  		$thumb->close();
  
  		$thumb->setFilename($prefix."small.jpg");
  		$thumb->open("write");
  		$thumb->write($thumbsmall);
  		$thumb->close();
  
  		$thumb->setFilename($prefix."medium.jpg");
  		$thumb->open("write");
  		$thumb->write($thumbmedium);
  		$thumb->close();
  
  		$thumb->setFilename($prefix."large.jpg");
  		$thumb->open("write");
  		$thumb->write($thumblarge);
  		$thumb->close();
  
  		$sector->icontime = time();
  	}
  }
  ///////////////////////////////////////////////////////////////////////////////////////////
  
  
  
  
  if (add_entity_relationship($sector_guid, 'sectorPerteneceEscuela', $escuela_guid)){
  		system_message("Sector asociado a escuela");
  }
  else {
  	register_error("No se pudo crear la relacion entre sector y escuela");
    forward(REFERER); // REFERER is a global variable that defines the previous page
  }
  
  
  if ($sector_guid) {
     system_message("Tu sector ha sido guardada");
     forward($sector->getURL());
  } else {
     register_error("El sector no pudo ser guardado");
     forward(REFERER); // REFERER is a global variable that defines the previous page
  }
?>